<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\PastureOrderProduct;
use app\common\model\PastureProduct;
use app\common\model\PastureProductSpecValue;

class PastureOperation
{
    public function handle($data)
    {
        $list = PastureOrderProduct::where('order_id', $data)->field('pid, spec_value_id, num')->select();

        foreach ($list as $v) {
            PastureProduct::where('id', $v->pid)->inc('sales', $v->num)->update();
            PastureProduct::where('id', $v->pid)->dec('stock', $v->num)->update();
            PastureProductSpecValue::where('id', $v->spec_value_id)->dec('stock', $v->num)->update();
        }
    }
}